package com.ruoyi.quartz.task;


import com.ruoyi.bizsys.domain.CreditAccount;
import com.ruoyi.bizsys.domain.LoanIou;
import com.ruoyi.bizsys.domain.RepaymentOrder;
import com.ruoyi.bizsys.service.ICreditAccountService;
import com.ruoyi.bizsys.service.IRepaymentOrderService;
import com.ruoyi.bizsys.utils.BizConstants;
import com.ruoyi.business.service.IBLoanInfoService;
import com.ruoyi.business.service.IBRepaymentService;
import com.ruoyi.common.core.redis.RedisCache;
import lombok.extern.slf4j.Slf4j;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.List;

@Slf4j
@Component("overflowTask")
public class OverflowTask {
    @Autowired
    ICreditAccountService iCreditAccountService;

    @Autowired
    IBLoanInfoService loanInfoService;

    @Autowired
    IBRepaymentService ibRepaymentService;

    @Autowired
    IRepaymentOrderService repaymentOrderService;

    @Autowired
    ICreditAccountService creditAccountService;

    @Autowired
    RedisCache redisCache;

    @Autowired
    RedissonClient redissonClient;

    public void overflowRepayment() {

        List<CreditAccount> creditAccountList = iCreditAccountService.getOverflowAmount();

        for (int i = 0; i < creditAccountList.size(); i++) {
            CreditAccount creditAccount = creditAccountList.get(i);
            //查找用户有无待还款记录
            List<LoanIou> loanIouList = loanInfoService.getFirstOverdueByCustNo(creditAccount.getCustNo());
            if (loanIouList != null && loanIouList.size() > 0) {
                //锁住客户账户再操作
                String lockKey = RedisCache.CUST_ACCOUNT_LOCK_KEY_PREFIX + creditAccount.getCustNo();
                RLock lock = redisCache.lock(lockKey);
                try {
                    //防止并发，再查询一下用户的
                    creditAccount = creditAccountService.selectCreditAccountById(creditAccount.getId());
                    if (!creditAccount.getOnLoan().equals(BizConstants.ACCOUNT_ON_lOAN_STATUS_1)) {
                        log.info("creditAccount:OnLoan==1" + creditAccount.getCustNo());
                        lock.unlock();
                        return;
                    }
                    long start = System.currentTimeMillis();
                    LoanIou loanIou = loanIouList.get(0);
                    RepaymentOrder repaymentOrder2 = ibRepaymentService.initRepaymentOrderToLoanIou(loanIou, BizConstants.LOAN_REPAYMENT_ORDER_TYPE_OVERFLOW, BigDecimal.ZERO);
                    ibRepaymentService.overflowRepayment(repaymentOrder2, creditAccount);
                    long end = System.currentTimeMillis();
                    log.info("锁占用时间，耗时：" + (end - start) + " ms");
                } catch (Exception e) {
                    log.error("Repayment 线程 sleep 异常", e);
                } finally {
                    lock.unlock();
                }
            }
        }

    }
}
